package com.xjk.string;

import java.math.BigDecimal;

public class AmountToChinese {

    private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CN_UPPER_MONETARY_UNIT = {"分", "角", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟"};
    private static final String CN_FULL = "整";
    private static final String CN_NEGATIVE = "负";
    private static final int MONEY_PRECISION = 2;
    private static final String CN_ZERO_FULL = "零圆" + CN_FULL;

    public static String numberToChinese(BigDecimal number) {
        StringBuilder sb = new StringBuilder();
        int signum = number.signum();
        if (signum == 0) {
            return CN_ZERO_FULL;
        }

        long numberLong = number.movePointRight(MONEY_PRECISION).setScale(0, BigDecimal.ROUND_HALF_UP).abs().longValue();
        long scale = numberLong % 100;
        int numUnit = 0;
        int numIndex = 0;
        boolean getZero = false;

        if (!(scale > 0)) {
            numIndex = 2;
            numberLong = numberLong / 100;
            getZero = true;
        } 
        if ((scale > 0) && !(scale % 10 > 0)) {
            numIndex = 1;
            numberLong = numberLong / 10;
            getZero = true;
        }

        int zeroSize = 0;
        while (numberLong > 0) {
            numUnit = (int) (numberLong % 10);
            if (numUnit > 0) {
                if ((numIndex == 9) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETARY_UNIT[6]);
                }
                if ((numIndex == 13) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETARY_UNIT[10]);
                }
                sb.insert(0, CN_UPPER_MONETARY_UNIT[numIndex]);
                sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                getZero = false;
                zeroSize = 0;
            } else {
                ++zeroSize;
                if (!getZero) {
                    sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                }
                if (numIndex == 2) {
                    if (numberLong > 0) {
                        sb.insert(0, CN_UPPER_MONETARY_UNIT[numIndex]);
                    }
                } else if (((numIndex - 2) % 4 == 0) && (numberLong % 1000 > 0)) {
                    sb.insert(0, CN_UPPER_MONETARY_UNIT[numIndex]);
                }
                getZero = true;
            }
            numberLong = numberLong / 10;
            ++numIndex;
        }

        if (signum == -1) {
            sb.insert(0, CN_NEGATIVE);
        }

        if (!(scale > 0)) {
            sb.append(CN_FULL);
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        BigDecimal amount1 = new BigDecimal("1234567890.56");
        BigDecimal amount2 = new BigDecimal("1001312221.00");
        BigDecimal amount3 = new BigDecimal("0.00");
        System.out.println("大写金额: " + numberToChinese(amount1));
        System.out.println("大写金额: " + numberToChinese(amount2));
        System.out.println("大写金额: " + numberToChinese(amount3));
    }
}